package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetUserInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.UserInfoModel;

public class SetUserInfoActivity extends BaseActivity implements View.OnClickListener {

    EditText setuserinfo_username,setuserinfo_birthday,setuserinfo_nickname,setuserinfo_signature;
    String userid;
    RadioButton setuserinfo_nan,setuserinfo_nv;
    Button setuserinfo_btn;
    RadioGroup setuserinfo_sex;
    ImageView setuserinfo_fh;


    int mYear, mMonth, mDay;
    TextView setuserinfo_dateChoose;
    final int DATE_DIALOG = 1;


    @Override
    void initView() {
        setLayout_file(R.layout.activity_set_user_info);
        setuserinfo_username = (EditText)findViewById(R.id.setuserinfo_username);
        setuserinfo_birthday = (EditText)findViewById(R.id.setuserinfo_birthday);
        setuserinfo_nickname = (EditText)findViewById(R.id.setuserinfo_nickname);
        setuserinfo_signature = (EditText)findViewById(R.id.setuserinfo_signature);

        setuserinfo_nan = (RadioButton)findViewById(R.id.setuserinfo_nan);
        setuserinfo_nv = (RadioButton)findViewById(R.id.setuserinfo_nv);

        setuserinfo_btn = (Button)findViewById(R.id.setuserinfo_btn);
        setuserinfo_sex = (RadioGroup)findViewById(R.id.setuserinfo_sex);
        setuserinfo_fh = (ImageView)findViewById(R.id.setuserinfo_fh);

        setuserinfo_dateChoose = (TextView)findViewById(R.id.setuserinfo_dateChoose);
        userid = getid();

        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.getUserInfo(userid,userInfoBeanTListener);


        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    void initEvent() {
        setuserinfo_btn.setOnClickListener(this);
        setuserinfo_fh.setOnClickListener(this);
        setuserinfo_dateChoose.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }
    TListener<UserInfoBean> userInfoBeanTListener = new TListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean userInfoBean) {
            String birthday = userInfoBean.getUser_birthday();
            String signature = userInfoBean.getUser_signature();
            String sex = userInfoBean.getUser_sex();
            if (sex.equals("1")){
               // setuserinfo_nan.isChecked();
                setuserinfo_nan.setChecked(true);
            }
            else{
                setuserinfo_nv.setChecked(true);
            }
            if (birthday == null){
                setuserinfo_birthday.setText("空");

            }
            else{
                setuserinfo_birthday.setText(userInfoBean.getUser_birthday());
            }
             if(signature == null){
                setuserinfo_signature.setText("空");
            }
            else{
                 setuserinfo_signature.setText(userInfoBean.getUser_signature());
             }
            setuserinfo_username.setText(userInfoBean.getUser_name());
            setuserinfo_nickname.setText(userInfoBean.getUser_nickname());
        }

        @Override
        public void onFail(String msg) {

        }
    };


    public void onClick(View view) {
        switch (view.getId()){
            case R.id.setuserinfo_btn:
                String username = setuserinfo_username.getText().toString().trim();
                String nickname = setuserinfo_nickname.getText().toString().trim();
                String sex =  setuserinfo_sex.getCheckedRadioButtonId() == R.id.setuserinfo_nan ? "1" : "0";
                String birthday = setuserinfo_birthday.getText().toString().trim();
                String signature = setuserinfo_signature.getText().toString().trim();

                if (username.equals("")||nickname.equals("")){
                    Toast.makeText(SetUserInfoActivity.this,"用户名或昵称不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    SetUserInfoModel setUserInfoModel = new SetUserInfoModel();
                    setUserInfoModel.setUserInfo(userid,username,nickname,sex,signature,birthday,successBeanTListener);
                }
                break;
            case R.id.setuserinfo_fh:
                finish();
                break;
            case R.id.setuserinfo_dateChoose:
                showDialog(DATE_DIALOG);
                break;
        }
    }

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(SetUserInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else if(success.equals("2")){
                Toast.makeText(SetUserInfoActivity.this,"账号已存在",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(SetUserInfoActivity.this,successBean.getSuccess(),Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SetUserInfoActivity.this,"111",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }
    /**
     * 设置日期 利用StringBuffer追加
     */
    public void display() {
        setuserinfo_birthday.setText(new StringBuffer().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(" "));
    }
    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };
}
